import {
  plans,
  pressures,
  userStatus,
  baseRoot
} from '../../api/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    plans: [],
    pressures: [],
    url: wx.getStorageSync('url'),
    aisResult: -1,
    pressureResult: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getPlans();
    this.getPressures();
    this.getUserStatus();
  },
  /**
   * 分享
   */
  onShareAppMessage: function() {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'
    }
  },

  /**
   * 点击睡眠计划的“查看更多”
   */
  handlePlanDetail(e) {
    console.log(e, 'eeeeee')
    let planId = e.currentTarget.dataset.id
    if (this.data.aisResult >= 0) {
      wx.navigateTo({
        url: '/pages/course-description/course-description?planId=' + planId,
      })
    } else {
      wx.navigateTo({
        url: '/pages/ais-evaluation/ais-evaluation',
      })
    }
  },

  /**
   * 点击压力课程的“查看更多” 
   */
  handlePressureDetail(e) {
    console.log(e, 'eeeeee')
    let pressureId = e.currentTarget.dataset.id
    if (this.data.pressureResult >= 0) {
      wx.navigateTo({
        url: '/pages/pressure-description/pressure-description?pressureId=' + pressureId,
      })
    } else {
      wx.navigateTo({
        url: '/pages/pressure-evaluation/pressure-evaluation',
      })
    }
  },

  getPlans() {
    plans('').then(res => {
      console.log(res)
      res.forEach(function(item, index) {
        switch (item.days) {
          case 7:
            item.image_src = baseRoot + '/static/images/more_plan_7.png'
            item.color = 'blue'
            item.li = 'li'
            break;
          case 14:
            item.image_src = baseRoot + '/static/images/more_plan_14.png'
            item.color = 'yellow'
            item.li = 'li'
            break;
          case 28:
            item.image_src = baseRoot + '/static/images/more_plan_28.png'
            item.color = 'orange'
            item.li='li1'


            break;
        }
        console.log(item)
      })
      this.setData({
        plans: res
      })
    })
  },

  getPressures() {
    pressures('').then(res => {
      console.log(res);
      res.forEach(function(item, index) {
        item.image_src = baseRoot + '/static/images/more_pressure.png'
        item.color = 'blue'
        item.li = 'li'
        console.log(item)
      })
      this.setData({
        pressures: res
      })
    })
  },

  getUserStatus() {
    userStatus().then(res => {
      console.log(res.errcode)
      if (res.errcode == '00000') {
        this.setData({
          aisResult: res.aisResult,
          pressureResult: res.pressureResult
        })
      }
    })
  },
})